#!/bin/bash

set -o errexit
set -o xtrace

test_dir=$(realpath $(dirname $0))
. ${test_dir}/../functions

max_conn=10

create_namespace $namespace
deploy_operator

desc 'create first PSMDB cluster'
cluster="some-name-rs0"
kubectl_bin apply \
    -f $conf_dir/secrets.yml \
    -f $conf_dir/client.yml

apply_cluster $conf_dir/$cluster.yml
desc 'check if all 3 Pods started'
wait_for_running $cluster 3

desc 'check if service and statefulset created with expected config'
compare_kubectl statefulset/$cluster
compare_kubectl service/$cluster

desc 'check if users created'
compare_mongo_user "userAdmin:userAdmin123456@$cluster.$namespace"
compare_mongo_user "backup:backup123456@$cluster.$namespace"
compare_mongo_user "clusterAdmin:clusterAdmin123456@$cluster.$namespace"
compare_mongo_user "clusterMonitor:clusterMonitor123456@$cluster.$namespace"
# check that test user don't have access
( run_mongo 'db.runCommand({connectionStatus:1,showPrivileges:true})' "test:test@$cluster.$namespace" || :) 2>&1 \
    | grep 'Authentication failed'

desc 'write data, read from all'
run_mongo \
    'db.createUser({user:"myApp",pwd:"myPass",roles:[{db:"myApp",role:"readWrite"}]})' \
    "userAdmin:userAdmin123456@$cluster.$namespace"
sleep 2
run_mongo \
    'use myApp\n db.test.insert({ x: 100500 })' \
    "myApp:myPass@$cluster.$namespace"
compare_mongo_cmd "find" "myApp:myPass@$cluster-0.$cluster.$namespace"
compare_mongo_cmd "find" "myApp:myPass@$cluster-1.$cluster.$namespace"
compare_mongo_cmd "find" "myApp:myPass@$cluster-2.$cluster.$namespace"

desc 'check number of connections'
conn_count=$(run_mongo 'db.serverStatus().connections.current' "clusterAdmin:clusterAdmin123456@$cluster.$namespace" | egrep -v 'I NETWORK|W NETWORK|Error saving history file|Percona Server for MongoDB|connecting to:|Unable to reach primary for set|Implicit session:|versions do not match|bye')
if [ ${conn_count} -gt ${max_conn} ];then
  exit 1
fi

desc 'kill Primary Pod, check reelection, check data'
initial_primary=$(get_mongo_primary "clusterAdmin:clusterAdmin123456@$cluster.$namespace" "$cluster")
kubectl_bin delete pods --grace-period=0 --force $initial_primary
wait_for_running $cluster 3
changed_primary=$(get_mongo_primary "clusterAdmin:clusterAdmin123456@$cluster.$namespace" "$cluster")
[ "$initial_primary" != "$changed_primary" ]
run_mongo \
    'use myApp\n db.test.insert({ x: 100501 })' \
    "myApp:myPass@$cluster.$namespace"
compare_mongo_cmd "find" "myApp:myPass@$cluster-0.$cluster.$namespace" "-2nd"
compare_mongo_cmd "find" "myApp:myPass@$cluster-1.$cluster.$namespace" "-2nd"
compare_mongo_cmd "find" "myApp:myPass@$cluster-2.$cluster.$namespace" "-2nd"

desc 'check if possible to create second cluster'
cluster2="another-name-rs0"
apply_cluster $test_dir/conf/$cluster2.yml
desc 'check if all 3 Pods started'
wait_for_running $cluster2 3
desc 'check if service and statefulset created with expected config'
compare_kubectl statefulset/$cluster2
compare_kubectl service/$cluster2

desc 'write data, read from all'
run_mongo \
    'db.createUser({user:"myApp",pwd:"myPass",roles:[{db:"myApp",role:"readWrite"}]})' \
    "userAdmin:userAdmin123456@$cluster2.$namespace"
run_mongo \
    'use myApp\n db.test.insert({ x: 100502 })' \
    "myApp:myPass@$cluster2.$namespace"
compare_mongo_cmd "find" "myApp:myPass@$cluster2-0.$cluster2.$namespace" "-3rd"
compare_mongo_cmd "find" "myApp:myPass@$cluster2-1.$cluster2.$namespace" "-3rd"
compare_mongo_cmd "find" "myApp:myPass@$cluster2-2.$cluster2.$namespace" "-3rd"

destroy $namespace
